<?php

class MeetingAction extends Action {
	
	/*
	 * 在构造函数中装载所依赖的类或其他文件
	*/
	function __construct() {
		include_once module_php_path('meeting', 'meeting', 'class.php');
	}
	
	//取得会议列表信息
	public function getMeetingList($order="id", $orderBy="desc", $start = 0, $limit = 25) {
				
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		
		$MeetingInfo = M("module_meeting");
		$cid = Session::$user["customerId"];
		$condition["deleted"] = array("eq",0);
		$arr = $MeetingInfo->where($condition)->order("$order $orderBy")->limit("$start, $limit")->select();
		if ($arr) {
			$count = $MeetingInfo->where($condition)->count();
			Output::instance()->objArray($arr, $count);
		} else {
			Output::instance()->resultCode(code_success);
		}
	}    
    
    public function getMeeting($id){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		
		$MeetingInfo=M("module_meeting");	
	
		$cid = Session::$user["customerId"];
		$condition["id"] = array("eq",$id);
		$meeting = $MeetingInfo->where($condition)->find();
		if ($meeting) {
			$meeting["materialCfgName"]="";
			
			$materialModel=M("material");
			$material = $materialModel->where("id=".$meeting["collectCfgId_Register"])->find();
			if ($material){
			  $meeting["materialCfgName"]=$material["name"];
			}
			
			Output::instance()->object($meeting);
		} else {
			Output::instance()->resultCode(code_success);
		}
	}
   
       
	public function addMeeting($name=NULL,$checkOkReply=NULL,$checkRefuseReply=NULL,$signOkReply=NULL,$collectCfgId_Register=NULL,$theme = NULL,$content = NULL,$startTime = NULL,$schedule = NULL,$place = NULL){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		
		$MeetingModel = M("module_meeting");
		$cid = Session::$user["customerId"];
		
		$time = date("Y-m-d H:i:s");
		$data["name"] = $name;
		$data["checkOkReply"] = $checkOkReply;
		$data["checkRefuseReply"] = $checkRefuseReply;
		$data["signOkReply"]=$signOkReply;
		$data["collectCfgId_Register"]=$collectCfgId_Register;
		$data["theme"] = $theme;
		$data["content"] = $content;
		$data["startTime"] = $startTime;
		$data["schedule"] = $schedule;
		$data["place"] = $place;
		$data["createTime"] = $time;

		$result = $MeetingModel->add($data);
		if ($result) {
		  $data["id"] = $result;
		  Output::instance()->object($data);
		} else {
		  Output::instance()->resultCode(code_sql_error);
		}		
	}
    
    public function deleteMeeting($id){		
				
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		
		$meetingReg = M("module_meeting");
		$result = $meetingReg->where("id=$id")->setField("deleted", 1);
		if ($result) {
			Output::instance()->resultCode(code_success);			
		} else {
			Output::instance()->resultCode(code_sql_error);
		}
	}
       
       
       
 
	public function updateMeeting($id,$name = NULL,$checkOkReply=NULL,$checkRefuseReply=NULL,$signOkReply=NULL,$theme = NULL,$content = NULL,$collectCfgId_Register=NULL,$startTime = NULL,$schedule = NULL,$place = NULL){
					
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}		
		
		$meeting = M("module_meeting");
		$cid = Session::$user["customerId"];
		
		$time = date("Y-m-d H:i:s");
		$data["name"] = $name;
		
		$data["checkOkReply"] = $checkOkReply;
		$data["checkRefuseReply"] = $checkRefuseReply;
		$data["signOkReply"]=$signOkReply;
		
		$data["collectCfgId_Register"]=$collectCfgId_Register;
		$data["theme"] = $theme;
		$data["content"] = $content;
		$data["startTime"] = $startTime;
		$data["schedule"] = $schedule;
		$data["place"] = $place;
		$data["updateTime"] = $time;
        
		$result = $meeting->where("id=$id")->save($data);
		if ($result!=false) {
		  $data["id"] = $id;
		  Output::instance()->object($data);
		} else {
		  Output::instance()->resultCode(code_sql_error);
		}		
	}
	
	
	
	public static function getMeetingRegList($materialId,$startDate='',$endDate='',$returnJson=true, $start = 0, $limit = 10,$order="id", $orderBy="asc"){
		$CIResultModel = M("collect_information_result");
		
		$retVal = Session::init();
		if (!$retVal) {
			if($returnJson){
				Output::instance()->resultCode(code_access_denied);
				return;
			}else{
				return null;
			}
		}
	
		$cid = 	Session::$user['customerId'];
		$condition['fromCustomerId'] = array('eq',$cid);
		$condition['materialId'] = array('eq',$materialId);
		
		
		if($startDate != ''){
			$condition['updateTime'] = array('between',array($startDate,$endDate));
		}else{
			$condition['updateTime'] = array('exp','is not null');
		}
		
		
		
		if($limit == null){
			$arr = $CIResultModel
			->where($condition)
			->order("$order $orderBy")
			->select();
		}else{
			$arr = $CIResultModel
			->where($condition)
			->order("$order $orderBy")
			->limit("$start, $limit")
			->select();
		}
		
		
        for($i=0;$i < count($arr);$i++){
          $id=$arr[$i]["id"];
          $arr[$i]["checkStatus"]=NULL;
          $arr[$i]["checkTime"]=NULL;
          $arr[$i]["signStatus"]=NULL;
          $arr[$i]["signTime"]=NULL;
          
          $meetingReg=self::getMettingRegById($id);
          if ($meetingReg){
            $arr[$i]["checkStatus"]=$meetingReg["checkStatus"];
            $arr[$i]["checkTime"]=$meetingReg["checkTime"];
            $arr[$i]["signStatus"]=$meetingReg["signStatus"];
            $arr[$i]["signTime"]=$meetingReg["signTime"];
          }              
        }
		
		if($arr){
		  $count = $CIResultModel->where($condition)->count();
		  Output::instance()->objArray($arr,$count);
		}else{
		  Output::instance()->resultCode(code_success);
		}
	}
	
	
	public function check($meetingRegId,$meetingId,$checkStatus){				
		$retVal = Session::init();
		$customerId = Session::$user["customerId"];
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}

	    Session::switchCustomer(0);
		$CIResultModel = M("collect_information_result");
		$CIResult=$CIResultModel->where("id=$meetingRegId")->find();
		$fromUserName=$CIResult["fromUserName"];
		Session::switchCustomer($customerId);
		
		$time = date("Y-m-d H:i:s");
		$MeetingRegModel = M("module_meeting_register");
		$result = $MeetingRegModel->where("collectResultId=$meetingRegId")->find();
		if ($result) {
			$MeetingRegModel->where("collectResultId=$meetingRegId")->setField("checkStatus", $checkStatus);
			$MeetingRegModel->where("collectResultId=$meetingRegId")->setField("checkTime", $time);
		} else {
			$data["fromUserName"]=$fromUserName;
			$data["collectResultId"] = $meetingRegId;
			$data["meetingId"] = $meetingId;
			$data["checkStatus"] = $checkStatus;
			$data["checkTime"] = $time;
			$result = $MeetingRegModel->add($data);
		}
		
		//处理审核的消息
        $meeting=meeting::getMeetingById($meetingId);
        if ($meeting){
          $text="";
          if ($checkStatus==1){
            $text=$meeting["checkOkReply"];
          } else {
            $text=$meeting["checkRefuseReply"];
          }
          $text = str_replace('{$meetingName}', $meeting['name'], $text);
          $text = str_replace('{$checkTime}', date("Y-m-d H:i:s"), $text);
          
          if ($text!==""){
            WeixinApi::sendText($fromUserName,$text);
          }
        }
				
		Output::instance()->resultCode(code_success);
	}
	
		
	public function getMettingRegById($collectResultId){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		
		$cid = Session::$user["customerId"];
		
		$MeetingRegModel = M("module_meeting_register");
		//$condition["meetingId"] = array("eq",$meetingId);
		$condition["collectResultId"] = array("eq",$collectResultId);
		$meetingReg = $MeetingRegModel->where($condition)->distinct(true)->find();
		
		return $meetingReg;
	}
	
	public function createQRCode($meetingId) {
        Log::write("meetingId=".$meetingId);

		if (!Session::init()) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}

		Log::write("meetingId 22222");
		$qrcode = QRCode::getByModule('meeting', $meetingId);
		Log::write("meetingId 33333");
		if (!isset($qrcode)) {
			Log::write("meetingId 44444");		
			
			$MeetingInfo=M("module_meeting");	
			$condition["id"] = array("eq",$meetingId);
			$meeting = $MeetingInfo->where($condition)->find();
			
			$qrcode = QRCode::add($meeting['name'], 'meeting', $meetingId, 'activitySign');
			Log::write("meetingId 55555");
		}
		
		Log::write("meetingId 66666=".$qrcode["img"]);
		Output::instance()->object($qrcode);
	}
	
}
